import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  // 内容
  state: {
    frames: [
      { name: 'Vue.js', star: 15 },
      { name: 'React.js', star: 13 },
      { name: 'Angular.js', star: 12 }
    ]
  },
  getters: {
    varyFrames: function (state) {
      var varyFrames = state.frames.map(frames => {
        return {
          name: '**' + frames.name + '**',
          star: frames.star
        }
      }
      )
      return varyFrames
    }
  },
  mutations: {
    addStar: function (state) {
      state.frames.map(function (frames) {
        frames.star += 1
      })
    },
    addStar1: function (state, payload) {
      state.frames.map(function (frames) {
        frames.star += payload
      })
    }
  },
  actions: {
    addStar1: (context, payload) => {
      setTimeout(function () {
        context.commit('addStar1', payload)
      }, 2000)
    }
  }
})

export default store
